package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class ManagerBlogServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		doGet(request,response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if (dbObject == null) {
			try {
				response.sendRedirect("/");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		DBCollection collections = Data.data("users");
		
		dbObject = collections.findOne(new BasicDBObject("username",dbObject.get("username").toString()));
		session.removeAttribute("users");
		session.setAttribute("users", dbObject);
		
		String name = request.getParameter("name");
		String type = request.getParameter("type");

		String page = request.getParameter("p");
		int p = Paginate.page(page);
		int limit = 20;
		int start = (p - 1) * limit;

		String remove = request.getParameter("remove");
		request.setAttribute("remove", remove);

		String falses = request.getParameter("false");
		request.setAttribute("falses", falses);

		DBCollection collection = Data.data("blogCraw");
		int total = 0;
		DBCursor cursor = null;
		String path = "?";
		if (name == null) {
			cursor = collection
					.find(new BasicDBObject("username", dbObject
							.get("username"))).skip(start).limit(limit);

			total = collection.find(
					new BasicDBObject("username", dbObject.get("username")))
					.count();
		} else {
			if (type.equals("")) {
				cursor = collection
						.find(new BasicDBObject("username", dbObject
								.get("username")).append("blogurl",
								new BasicDBObject("$regex", name))).skip(start)
						.limit(limit);
				total = collection.find(
						new BasicDBObject("username", dbObject.get("username"))
								.append("blogurl", new BasicDBObject("$regex",
										name))).count();
				path = "?name=" + name + "&";
			} else {
				cursor = collection
						.find(new BasicDBObject("username", dbObject
								.get("username")).append("blogurl",
								new BasicDBObject("$regex", name)).append(
								"type", type)).skip(start).limit(limit);
				total = collection.find(
						new BasicDBObject("username", dbObject.get("username"))
								.append("blogurl",
										new BasicDBObject("$regex", name))
								.append("type", type)).count();
				path = "?name=" + name + "&type=" + type + "&";
			}

		}
		int np = (total % limit == 0) ? total / limit : total / limit + 1;

		DBCollection coll = Data.data("urlCraw");
		long sumSource = coll.count(new BasicDBObject("username", dbObject
				.get("username")).append("status", "true"));
		long sum = coll.count(new BasicDBObject("username", dbObject
				.get("username")));

		request.setAttribute("sumSource", String.valueOf(sumSource));
		request.setAttribute("sum", String.valueOf(sum));
		request.setAttribute("total", String.valueOf(total));
		request.setAttribute("page", String.valueOf(p));
		request.setAttribute("np", String.valueOf(np));
		request.setAttribute("path", path);

		request.setAttribute("cursor", cursor);

		try {
			request.getRequestDispatcher("/manager_blog.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
